<?php
/**
 * Created by PhpStorm.
 * Author: 黑瞎子
 * Date: 2018/3/16
 * Time: 21:53
 */

namespace app\api\controller\v1;


use app\api\model\ProductImage;
use app\api\validate\Count;
use app\api\model\Product as ProductModel;
use app\api\model\Image as ImageModel;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\PagingParameter;
use app\api\validate\ProductNew;
use app\lib\exception\ParameterException;
use app\lib\exception\ProductException;
use app\lib\exception\SuccessMessage;
use app\lib\exception\TokenException;
use think\Cache;
use think\Db;
use think\Exception;
use app\api\service\Product as ProductService;

class Product
{
    /*
     * 最新商品
     * @url /porduct/recent?count=15
     * */
    public function getRecent($count = 15){
        (new Count())->goCheck();
        $products = ProductModel::getMostRecent($count);
        if ($products->isEmpty()){
            throw new ProductException();
        }
        $products = $products->hidden(['summary']);
        return $products;
    }

    /*
     * 通过分类id获取该分类下的商品信息
     * @url /product/by_category?id=2
     * */
    public function getAllCategory($id){
        (new IDMustBePostiveInt())->goCheck();
        $products = ProductModel::getProductByCategory($id);
        if ($products->isEmpty()){
            throw new ProductException();
        }
        return $products;
    }

    /*
     * 商品详情页
     * @url /product/:id
     * */
    public function getOne($id){
        (new IDMustBePostiveInt())->goCheck();
        $product = ProductModel::getProductDetail($id);
        if (!$product){
            throw new ProductException();
        }
        return $product;
        //隐藏返回数据字段
    }

    /*
     * cms 获取商品信息列表 -分页获取
     * @url /product/all
     * @GET
     * @params: $page int 第几页
     *          $size int 每页条数
     * return array
     * */
    public function getProductsByPage($page = 1, $size = 20){
        (new PagingParameter())->goCheck();
        $product = new ProductService();
        $result = $product->getProductsByPage($page, $size);
        return $result;
    }

    /*
     * cms 商品添加或者更新
     * @url /product
     * @POST
     * return 更新成功
     * */
    public function createOrUpdateProduct(){
        $validata = new ProductNew();
        $validata->goCheck();
        $dataArray = $validata->getDateByRuleByKey(input('post.'));
        $product = new ProductService();
        $result = $product->createOrUpdateProduct($dataArray);

        return $result;
    }

    /*
     * cms 给商品添加主图片
     * @url /prodct/main_img
     * @POST
     * @id 商品id
     * */
    public function addProductImgByID($id){
        (new IDMustBePostiveInt())->goCheck();

        $product = new ProductService();
        $result = $product->addProductImgByID($id, 'image');

        return $result;
        /*$dataArray = [];
        $imgInfo = $this->getUploadImageName('image', 1);
        if ($imgInfo) {
            $image = new ImageModel();
            $image->save($imgInfo);

            $dataArray['main_img_url'] = $imgInfo['url'];
            $dataArray['from'] = $imgInfo['from'];
            $dataArray['img_id'] = $image->id;
        }
        $product = new ProductModel();
        $product = $product->get($id);
        if (!$product){
            throw new ProductException();
        }
        $product->save($dataArray);
        return [
            'main_img_url' => $product['main_img_url']
        ];*/
    }

    /*
     * 删除商品 软删除
     * @delete @url /product
     * @id 商品id
     * */
    public function deleteProductByID($id){
        (new IDMustBePostiveInt())->goCheck();
        $product = new ProductService();
        $result = $product->deleteProductByID($id);
        return $result;
    }

    // 获取上传图片的名称
    /*public function getUploadImageName($img, $from){
        $file = request()->file($img);
        if($file){
            $info = $file->validate(['size'=>1567899,'ext'=>'jpg,png,gif'])
                ->move(ROOT_PATH . 'public' . DS . 'images', '');
            if($info){
                $infomsg = $info->getSaveName();
                return [
                    'url' => '/' . $infomsg,
                    'from' => $from
                ];
            }else{
                return $file->getError();
            }
        }else{
            throw new ParameterException([
                'msg' => '上传的文件为空'
            ]);
        }
    }*/

}